import redis
from urllib import request as ur
import time

class ProxyPool():
    def __init__(self):
        self.redis_conn = redis.StrictRedis(
            host='localhost',
            port=6379,
            decode_responses=True
        )

    def set_proxy(self):
        proxy_old = None

        while True:
            time.sleep(3)
            # 获取ip
            proxy_new = ur.urlopen('http://api.ip.data5u.com/dynamic/get.html?order=40ceffa0a1a630c860d060502009a264&sep=3').read().decode('utf-8').strip().split(' ')

            # 判断ip是否更新
            if proxy_new != proxy_old:
                proxy_old = proxy_new
                self.redis_conn.delete('proxy')
                self.redis_conn.sadd('proxy', *proxy_new)

                print('更换ip为：', proxy_new)
                time.sleep(2)
            else:
                time.sleep(1)

    def get_proxy(self):
        proxy_s = self.redis_conn.srandmember('proxy', 1)
        # print('proxy_s', proxy_s)
        # print(type(proxy_s))
        # 获取ip
        if proxy_s:
            return proxy_s[0]
        else:
            time.sleep(0.1)
            return self.get_proxy()


if __name__ == '__main__':
    ProxyPool().set_proxy()
    # ProxyPool().get_proxy()